package com.aifast.common.util;

import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @Author fujian
 * @since 2022/2/28 17:23
 */
public class ResponseUtil {

    private final static Logger log = LoggerFactory.getLogger(ResponseUtil.class);

    public static void responseJson(HttpServletResponse response, Object jsonObject) throws IOException {
        try {
            response.setContentType("application/json; charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(JSONUtil.toJsonStr(jsonObject).getBytes("UTF-8"));
            outputStream.flush();

        } catch (IOException e) {
            log.error("error={}",e);
        }
    }

    public static void turnToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect(request.getContextPath() + "/view/login");
    }

}
